package com.pro.staffmanage.config;

import com.pro.staffmanage.Interceptor.LoginHandlerInterceptor;
import com.pro.staffmanage.resolvers.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author: Haotian Cui
 * @Date: 2022/7/22 - 07 - 22 - 16:25
 * @Description: com.pro.staffmanage.config
 * @Project: staffmanage
 * @version: 1.0
 */
@Configuration
public class MyWebMvcConfiguration implements WebMvcConfigurer {

    //  Add WebMvcConfigurer built-in component
    /** Add view object (easy to enter url jump page, using RESTful style)
     * Equivalent to writing in Controller, writing in config can avoid controller confusion
     *     @RequestMapping({"/","/index"})
     *     public String wrapIndex(){
     *         return "index";
     *     }
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //  templates/index.html 会自动作为 / 出现
        //  wrap page: /emp/list.html
        registry.addViewController("/emplist").setViewName("emp/list");
        registry.addViewController("/deplist").setViewName("dep/list");
        //  wrap page: error/404.html
        registry.addViewController("/404").setViewName("error/404");
        registry.addViewController("/401").setViewName("error/401");

    }

    //  这里的 i18n 还是通过 web传统开发做的，虽然简化了前端流程，但却增加了前端对后端的侵入，很不舒服
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

//    有 Security 了就不用它了
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        //  Step description:
//        //      Add a self-built interceptor ->
//        //      specify the interception range (indicates all) ->
//        //      specify the exclusion of interception
//        registry.addInterceptor(new LoginHandlerInterceptor())
//                .addPathPatterns("/**")
//                .excludePathPatterns("/","/reg","/index","/css/**","/js/**","/img/**");
//    }

    //  Bean Injection -- Add WebMvcConfigurer not embedded components
    //  LocaleResolver

}
